package model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Aluno;
import model.Professor;

public class AlunoDAO {
	
	private EntityManager manager = JPAUtil.getEntityManager();
	
	
	public static AlunoDAO getInstance(){
		return new AlunoDAO();
	}

	private AlunoDAO() {
	}
	
	public void adiciona(Aluno aluno){
		JPAUtil.iniciaTransacao();
		manager.persist(aluno);
		JPAUtil.finalizaTransacao();
	}
	
	public Aluno buscaAluno(String login){
		Query query = manager.createQuery("SELECT p FROM Aluno p WHERE p.login = ?");
		query.setParameter(1, login);
		Aluno aluno = null;
		if (query.getResultList()!=null && !query.getResultList().isEmpty()){
			System.out.println(query.getResultList().isEmpty() + "result" + query.getMaxResults());
			aluno = (model.Aluno) query.getResultList().get(0);
		}
		return aluno;
		
	}
	
	public List<Aluno> buscarTodosAluno(){
		Query query = manager.createQuery("SELECT p FROM Aluno p");
		return query.getResultList();
	}
}
